package com.zmn.plat.services.impl.channel;

import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.model.entity.channel.ChannelServProduct;
import com.zmn.plat.model.vo.channel.ChannelServProductVO;
import com.zmn.plat.model.vo.product.tariff.item.price.ProductServiceItemExcutePriceQueryVo;
import com.zmn.plat.persistence.interfaces.channel.ChannelServProductDAO;
import com.zmn.plat.services.interfaces.channel.ChannelServProductService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 描述: 渠道服务产品表接口实现类
 *
 * @author linfeng
 * @since 2019/11/12
 **/
@Service
public class ChannelServProductServiceImpl implements ChannelServProductService {

    @Resource
    private ChannelServProductDAO channelServProductDAO;


    /**
     * 描述: 根据id查询渠道服务产品表数据
     *
     * @param id
     *            渠道服务产品表id
     * @return com.zmn.plat.model.entity.channel.ChannelServProduct
     * @author linfeng
     * @since 2019/11/12
     **/
    @Override
    public ChannelServProduct findById(Integer id) {
        return channelServProductDAO.findById(id);
    }

    /**
     * 描述: 根据产品id 查询数据 * * @param productId 产品id
     *
     * @return com.zmn.plat.model.entity.channel.ChannelServProduct
     * @author linfeng
     * @since 2019/11/12
     **/
    @Override
    public ChannelServProduct findByProductId(Integer productId, Integer channelId) {
        return channelServProductDAO.findByProductId(productId, channelId);
    }

    /**
     * 描述: 根据渠道ID和产品id集合 查询数据
     *
     * @param productIdList
     *            产品ID集合
     * @param channelId
     *            渠道ID
     * @return
     * @auther linfeng
     * @since 2019/11/12
     */
    @Override
    public List<ChannelServProduct> listByProductIdListAndChannelId(List<Integer> productIdList, Integer channelId) {
        return channelServProductDAO.listByProductIdListAndChannelId(productIdList, channelId);
    }

    /**
     * 描述: 根据渠道服务产品表id集合查询渠道服务产品表数据 如ids为null则返回 new ArrayList();
     *
     * @param ids
     *            渠道服务产品表id集合
     * @return java.util.List<com.zmn.plat.model.entity.channel.ChannelServProduct>
     * @author linfeng
     * @since 2019/11/12
     **/
    @Override
    public List<ChannelServProduct> listByIds(List<Integer> ids) {

        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return channelServProductDAO.listByIds(ids);
    }

    @Override
    public List<ChannelServProduct> listProductIdByChannelId(Integer channelId) {

        if (Objects.isNull(channelId)){
            return Collections.emptyList();
        }
        return channelServProductDAO.listProductIdByChannelId(channelId);
    }

    @Override
    public List<ChannelServProductVO> listProductByPriceQuery(ProductServiceItemExcutePriceQueryVo vo) {
        return channelServProductDAO.listProductByPriceQuery(vo);
    }

    /**
     * 描述: 新增渠道服务产品数据 注意channelServProductId为必传，否则抛出 NullPointerException();
     *
     * @param channelServProduct
     *            渠道服务产品数据
     * @author linfeng
     * @since 2019/11/12
     **/
    @Override
    public void updateChannelServProduct(ChannelServProduct channelServProduct) {

        if (Objects.isNull(channelServProduct.getChannelServProductId())) {
            throw new NullPointerException("channelServProductId为必传");
        }
        channelServProductDAO.updateChannelServProduct(channelServProduct);
    }

    @Override
    public void updateStatus(String ids, int status, int channelId, String realName) {
        List<Integer> productIdList = StringUtil.convertStrsToList(ids);
        List<ChannelServProduct> channelServProductList = this.listByProductIdListAndChannelId(productIdList, channelId);
        // 当前渠道已存在的产品ID集合
        List<Integer> productIdHave = channelServProductList.stream().map(ChannelServProduct::getProductId).collect(Collectors.toList());

        // 当前渠道没有对应服务产品表的产品ID集合
        ChannelServProduct channelServProduct;
        List<ChannelServProduct> channelServProducts = new ArrayList<>();
        for (int i = 0; i < productIdList.size(); i++) {
            if (productIdHave.contains(productIdList.get(i))) {
                continue;
            }
            channelServProduct = new ChannelServProduct();
            channelServProduct.setProductId(productIdList.get(i));
            channelServProduct.setChannelId(channelId);
            channelServProduct.setStatus(status);
            channelServProduct.setCreater(realName);
            channelServProduct.setCreateTime(DateUtil.getNow());
            channelServProducts.add(channelServProduct);
        }

        // 修改原来的服务产品表状态
        if (!CollectionUtils.isEmpty(productIdHave)) {
            channelServProductDAO.updateStatusByProductIdListAndChannelId(productIdHave, status, channelId, realName, DateUtil.getNow());
        }

        // 新增服务产品表
        if (!CollectionUtils.isEmpty(channelServProducts)) {
            this.insertChannelServProductBatch(channelServProducts);
        }
    }

    /**
     * 描述: 修改渠道服务产品数据
     *
     * @param channelServProduct
     *            渠道服务产品数据
     * @author linfeng
     * @since 2019/11/12
     **/
    @Override
    public void updateChannelServProductByProductId(ChannelServProduct channelServProduct) {

        if (Objects.isNull(channelServProduct.getProductId())) {
            throw new NullPointerException("productId为必传");
        }
        channelServProductDAO.updateChannelServProductByProductId(channelServProduct);
    }

    private void insertChannelServProductBatch(List<ChannelServProduct> channelServProductList) {
        channelServProductDAO.insertChannelServProductBatch(channelServProductList);
    }

    /**
     * 描述: 新增渠道服务产品数据
     *
     * @param channelServProduct
     *            渠道服务产品数据
     * @author linfeng
     * @since 2019/11/12
     **/
    @Override
    public void insertChannelServProduct(ChannelServProduct channelServProduct) {
        channelServProductDAO.insertChannelServProduct(channelServProduct);
    }

    @Override
    public Integer deleteByChannelId(Integer channelId) {
        return channelServProductDAO.deleteByChannelId(channelId);
    }

    @Override
    public void copy(Integer originalChannelId, int targetChannelId, String operator) {

        channelServProductDAO.copy(originalChannelId, targetChannelId, operator);
    }
}